<?php

namespace app\merch\controller;

use think\admin\Controller;
use think\admin\service\AdminService;
use think\admin\helper\QueryHelper;
use app\merch\model\ImsEweiShopMerchUser;
use app\merch\model\ImsEweiShopMerchAccount;
use app\goods\model\ImsEweiShopGoods;

use think\admin\model\SystemAuth;
use think\admin\model\SystemBase;
use think\admin\model\SystemUser;
use think\facade\Request;

class Index extends Controller
{
    public function index()
    {
        /*! 判断当前用户的登录状态 */
        $this->login = AdminService::isLogin();
        /*! 菜单为空且未登录跳转到登录页 */
        if (empty($this->menus) && empty($this->login)) {
            // $this->redirect(sysuri('admin/login/index'));
        } else {
            $this->type = $this->get['type'] ?? '1';
            ImsEweiShopMerchUser::mQuery()->layTable(function () {
                $this->title = '商户管理';
            }, function (QueryHelper $query) {

                switch ($this->type) {
                    case '1':
                        // 加载对应数据列表
                        $query->where(['status'=>1]);
                        break;
                    case '2':
                        $query->where(['status'=>2]);
                        break;
                    case '-1':
                        $query->where(['status'=>-1]);
                        break;
                    default:
                        if ($this->type==='0') {
                            $query->where(['status'=>0]);
                        }
                        break;
                }

                // 数据列表搜索过滤
                $query->like('merchname|merchno|realname|mobile#keywords');
            });

            $this->fetch();
        }
    }

    public function type(){
        echo('开发中！！！！');
    }

    /**
     * 添加系统用户
     * @auth true
     */
    public function add()
    {
        $username = Request::param('username');
        if ($username) {
            $merchInfo=ImsEweiShopMerchUser::mk()->where('username','=',$username)->find();
            if ($merchInfo) {
                $this->error('该用户已存在，请确认后再试');
            }
        }
        ImsEweiShopMerchUser::mForm('form');
    }

    /**
     * 编辑用户
     * @auth true
     */
    public function edit()
    {
        ImsEweiShopMerchUser::mForm('form');
    }

    /**
     * 删除用户
     * @auth true
     */
    public function remove()
    {
        $this->_checkInput();
        $ids_srt = Request::param('id');
        $ids_arr = array_unique(array_filter(explode(',', $ids_srt)));

        $goodsCount = ImsEweiShopGoods::mk()->where('merchid','in',$ids_arr)->count();

        if ($goodsCount) {
            $this->error('您所选商户存在商品，请先删除商品！');
        }
        ImsEweiShopMerchUser::mDelete();
    }

    /**
     * 修改用户密码
     * @auth true
     */
    public function pass()
    {
        $this->_applyFormToken();
        if ($this->request->isGet()) {
            $this->verify = false;
            ImsEweiShopMerchUser::mForm('pass');
        } else {
            $data = $this->_vali([
                'id.require'                  => '用户ID不能为空！',
                'password.require'            => '登录密码不能为空！',
                'repassword.require'          => '重复密码不能为空！',
                'repassword.confirm:password' => '两次输入的密码不一致！',
            ]);
            $user = ImsEweiShopMerchUser::mk()->findOrEmpty($data['id']);
            if ($user->isExists() && $user->save(['password' => md5($data['password'])])) {
                sysoplog('系统用户管理', "修改用户[{$data['id']}]密码成功");
                $this->success('密码修改成功，请使用新密码登录！', '');
            } else {
                $this->error('密码修改失败，请稍候再试！');
            }
        }
    }

    /**
     * 修改用户状态
     * @auth true
     */
    public function status()
    {
        $this->_checkInput();
        ImsEweiShopMerchUser::mSave($this->_vali([
            'status.in:0,1,2'  => '状态值范围异常！',
            'status.require' => '状态值不能为空！',
        ]));
    }

    /**
     * 检查输入变量
     */
    private function _checkInput()
    {
        if (in_array('10000', str2arr(input('id', '')))) {
            $this->error('系统超级账号禁止删除！');
        }
    }

}
